package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.service.VideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author AIR
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class VideoController {

    @Autowired
    private VideoService videoService;

    //注入vodClient
    @Resource
    private VodClient vodClient;

    //根据小节id查询数据回显
    @GetMapping("getVideoById/{id}")
    public R getVideo(@PathVariable String id){
        EduVideo video = videoService.getById(id);
        return R.ok().data("video",video);
    }

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }

    //删除小节
    //TODO 后面这个方法需要完善：删除小节时候，同时把里面的视频删除
    @DeleteMapping("deleteVideo/{id}")
    public R deleteVideo(@PathVariable("id") String id){
        //根据小节id获取视频id，调用方法实现视频删除
        EduVideo eduVideo = videoService.getById(id);
        //判断小节里面是否有视频id
        if (!StringUtils.isBlank(eduVideo.getVideoSourceId())){
            //根据视频id，远程调用实现视频删除
            vodClient.remove(eduVideo.getVideoSourceId());
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }

    //修改小节
    @PutMapping("updateVideo/{id}")
    public R updateVideo(@PathVariable String id,@RequestBody EduVideo eduVideo){
        System.out.println(eduVideo);
        //先修改video的路径为空
        Boolean flag = videoService.getVideoById(id,eduVideo);
        if (flag){
            System.out.println("现在已经删除成功");
        }else {
            System.out.println("没有视频，可添加");
        }

        videoService.updateVideo(id,eduVideo);
        return R.ok();
    }
}
